<?php
$items=[];
function scan($dir){
    global $items;
    if (is_dir($dir)) {
        if ($dh = opendir($dir)) {
            while (($file = readdir($dh)) !== false) {
                if($file=='.' || $file=='..') continue;
                
                if(filetype($dir.$file)=='dir'){
                    scan($dir.$file.'/');
                }else{
                    $items[]="{$dir}{$file}";
                }
            }
            closedir($dh);
        }
    }
}
//自动生成路由项
$dir = "./src/views/";
scan($dir);
$items=array_map(function($v) use ($dir){
    return str_replace([$dir,'.vue'],['',''],$v);
},$items);
$strs=[];
foreach($items as $k=>$v){
    $tpl=<<<st
    {
    path: '/{$v}',
    component: () => import('@/views/{$v}.vue'),
    },
st;
    $strs[]=$tpl;
}
$finaljs=str_replace('//placeholder',implode("\n",$strs),file_get_contents("./src/router/template.txt"));
file_put_contents('./src/router/index.js',$finaljs);
$total=count($items);
echo "Success create {$total} routes\n";
